package cn.itsource.org.service.impl;

import cn.itsource.basic.base.domain.ShopReject;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import cn.itsource.basic.utli.EmailUtil;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmailUtil emailUtil;

    /**
     * 店铺注册
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //将员工信息添加，获取主键id
        employeeMapper.save(admin);
        //将店铺信息进行添加，获取主键id
        shop.setAdmin_id(admin.getId());
        super.save(shop);
        //System.out.println(shop.getId());
        //将店铺id更新到员工shop_id中
        admin.setShop_id(shop.getId());
        //System.out.println(admin);
        employeeMapper.update(admin);
    }

    @Override
    @Transactional
    public void disable(Long id) {
        Shop shop = shopMapper.selectByid(id);
        shop.setState(-1);
        shopMapper.update(shop);
    }

    @Override
    public void pass(ShopReject reject) throws Exception {
        Shop shop = shopMapper.selectByid(reject.getId());
        shop.setState(1);
        shopMapper.update(shop);
        Employee employee = employeeMapper.selectEmaliByshopID(shop.getId());
        emailUtil.SendEmail(employee,reject);
    }

    @Override
    public void reject(ShopReject reject) throws Exception {
        Shop shop = shopMapper.selectByid(reject.getId());
        shop.setState(-2);
        shopMapper.update(shop);
        Employee employee = employeeMapper.selectEmaliByshopID(shop.getId());
        emailUtil.SendEmail(employee,reject);
    }
}
